package com.example.administrator.managesystem.adapter;


import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.bean.LandListBean;
import com.example.administrator.managesystem.utils.GlobalConfig;
import com.example.administrator.managesystem.view.CameraGridDialog;
import com.example.administrator.managesystem.view.CircleImageView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class ListViewSlideAdapter extends BaseAdapter {

    private List<LandListBean.DataBean> land_list=new ArrayList<>();
    private Context context;

    public ListViewSlideAdapter(Context context){
        this.context=context;

    }
    public void addDatas(List<LandListBean.DataBean> land_list){
        this.land_list=land_list;
    }
    @Override
    public int getCount() {
        return land_list.size();
    }

    @Override
    public Object getItem(int position) {
        return land_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        ViewHolder viewHolder;
        if(convertView==null) {
            convertView=View.inflate(context, R.layout.land_recycler_item, null);
            viewHolder=new ViewHolder();
              viewHolder.land_img=convertView.findViewById(R.id.land_img);
              viewHolder.land_name=convertView.findViewById(R.id.land_name);
              viewHolder.land_type=convertView.findViewById(R.id.land_type);
              viewHolder.land_series=convertView.findViewById(R.id.land_series);
              viewHolder.land_location=convertView.findViewById(R.id.land_location);
              viewHolder.add_camera=convertView.findViewById(R.id.add_camera);
              viewHolder.camera_list_iv=convertView.findViewById(R.id.camera_list_iv);

            convertView.setTag(viewHolder);//store up viewHolder
        }else {

            viewHolder=(ViewHolder)convertView.getTag();
        }
      if(!land_list.get(position).getImage().isEmpty()){

          Picasso.with(context).load(GlobalConfig.ONLINE_HTTP+""+land_list.get(position).getImage()+"").into(viewHolder.land_img);
      }

        viewHolder.land_name.setText(land_list.get(position).getName());
        viewHolder.land_type.setText(land_list.get(position).getGoods_name());
        viewHolder.land_series.setText(land_list.get(position).getBatch_number());
        viewHolder.land_location.setText(land_list.get(position).getLocation());
        viewHolder.add_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("msg","land_list:"+land_list);
                Log.e("msg","land_list.get(position).getId():"+land_list.get(position).getId());

                  addListener.onAddClick(land_list.get(position).getId(),position);
            }
        });


        viewHolder.camera_list_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cameraListListener.onCameraListClick(land_list.get(position).getId(),position);
            }
        });
        return convertView;
    }

    private class ViewHolder{
        TextView land_name,land_type,land_series,land_location;
        ImageView add_camera,camera_list_iv;
        CircleImageView land_img;

    }


    public void removeData(int position) {
       land_list.remove(position);
        notifyDataSetChanged();

    }
    private OnAddClickListener addListener;
    public interface OnAddClickListener {
        void onAddClick(String land_id,int position);
    }
    public void setOnAddClickListener (OnAddClickListener listener) {
        this.addListener=listener;
    }

    private OnCameraListClickListener cameraListListener;
    public interface OnCameraListClickListener {
        void onCameraListClick(String land_id,int position);
    }
    public void setOnAddCameraClickListener (OnCameraListClickListener listener) {
        this.cameraListListener=listener;
    }

}
